import { defineStore } from "pinia";

// 定义InfoData Store
const useInfoDataStore = defineStore("infoData", {
  state: () => ({
    dictionaries: {},
    // 当前航班信息
    flightInfo: {},

    // 当前DMT数据
    dmtData: {
      altitude: "",
      temperature: "",
      humidity: "",
      airPressure: "",
      windSpeed: "",
      windDirection: "",
      speed: "",
      direction: "",
      longitude: "",
      latitude: "",
      timestamp: "",
      updateTime: "",
    },

    // 历史数据
    historyDataList: [],
    historyIndex: 0,

    // 当前活动标签
    activeTab: "flight",

    // 当前模式
    currentMode: "",

    // 各个传感器设备的数据
    sensorData: {
      "AIMMS-30": {},
      CDP: {},
      CIP: {},
      PIP: {},
      LWC: {},
      PCASP: {},
      CCN: {},
      FCDP: {},
    },

    // 当前活动的DMT设备
    activeDmtDevice: "AIMMS-30",
  }),

  getters: {
    // 获取当前模式下的数据
    currentData: (state) => {
      if (state.historyDataList.length > 0) {
        if (state.historyIndex == 0) {
          state.flightInfo = state.historyDataList[0];
        } else {
          state.flightInfo = state.historyDataList.find(
            (item) => item.timeStamp == state.historyIndex
          );
        }
      }
      return state.flightInfo;
    },

    paramData: (state) => {

      
      if (state.currentMode == "realtime"){
        console.log(state.historyDataList);
        
        return state.historyDataList;
      }
        return state.historyDataList.filter(
          (item) => item.timeStamp <= state.historyIndex
        );
    },
  },

  actions: {
    // 设置字典数据
    getDictionaries(dict) {
      return this.dictionaries
        .find((item) => item.type === "AIRPLANE")
        .elements.find((item) => item.label === dict).abbr;
    },
    // 设置航班信息
    setFlightInfo(payload) {
      if (!payload) return;

      this.flightInfo = {
        ...this.flightInfo,
        ...payload,
      };
    },

    // 更新DMT数据
    updateDmtData(payload) {
      if (!payload) return;

      this.dmtData = {
        ...this.dmtData,
        ...payload,
        updateTime:
          payload.timestamp || payload.updateTime || new Date().toISOString(),
      };
    },

    // 设置历史数据
    setHistoryData(dataList) {
      this.historyDataList = dataList;
      this.historyIndex = 0;
    },

    // 设置历史数据索引
    setHistoryIndex(index: number) {
      if (index >= 0 && index < this.historyDataList.length) {
        this.historyIndex = index;
      }
    },

    // 切换标签
    setActiveTab(tab: string) {
      this.activeTab = tab;
    },

    // 切换模式（历史/实时）
    setMode(mode: "history" | "realtime") {
      this.currentMode = mode;
    },

    // 更新传感器设备数据
    updateSensorData(device: string, data: any) {
      if (!this.sensorData[device]) {
        this.sensorData[device] = {};
      }

      this.sensorData[device] = {
        ...this.sensorData[device],
        ...data,
      };
    },

    // 设置当前活动的DMT设备
    setActiveDmtDevice(device: string) {
      this.activeDmtDevice = device;
    },

    // 重置数据
    resetData() {
      this.flightInfo = {
        flightNo: "",
        date: "",
        takeoffTime: "",
        airplane: "",
      };

      this.dmtData = {
        altitude: "",
        temperature: "",
        humidity: "",
        airPressure: "",
        windSpeed: "",
        windDirection: "",
        speed: "",
        direction: "",
        longitude: "",
        latitude: "",
        timestamp: "",
        updateTime: "",
      };

      this.historyDataList = [];
      this.historyIndex = 0;
      this.activeTab = "flight";
      this.currentMode = "realtime";
    },
  },
});

export default useInfoDataStore;
